]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
screenshot: Use GLib.BookmarkFile to save recent screenshots
authorCarlos Garnacho <carlosg@gnome.org>
Wed, 22 Feb 2023 10:45:26 +0000 (11:45 +0100)
committerMarge Bot <marge-bot@gnome.org>
Wed, 1 Mar 2023 16:09:16 +0000 (16:09 +0000)
Avoid the GTK dependency, and use the common GLib API to store
screenshots in recent files. While at it, give it a better
exec hint than the implicit "gnome-shell %u" GTK added for us,
nobody seems to pay attention to that, but if they ever did
it's better to provide sensible information.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2656>

js/ui/screenshot.js

index b2ea6fad2ba5ba06d7861af1828e9f1cb820a737..bf54eb0691a9d6157f9bd5f2011b6e2580b29ec4 100644 (file)
@@ -1,7 +1,7 @@
 // -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
 /* exported ScreenshotService, ScreenshotUI, showScreenshotUI, captureScreenshot */
 
-const { Clutter, Cogl, Gio, GObject, GLib, Graphene, Gtk, Meta, Shell, St } = imports.gi;
+const {Clutter, Cogl, Gio, GObject, GLib, Graphene, Meta, Shell, St} = imports.gi;
 
 const GrabHelper = imports.ui.grabHelper;
 const Layout = imports.ui.layout;
@@ -2060,6 +2060,33 @@ function _storeScreenshot(bytes, pixbuf) {
             yield `-${i}`;
     }
 
+    /**
+     * Adds a record of a screenshot file in the recently used files list.
+     *
+     * @param {Gio.File} screenshotFile - The screenshot file.
+     */
+    function saveRecentFile(screenshotFile) {
+        const recentFile =
+            GLib.build_filenamev([GLib.get_user_data_dir(), 'recently-used.xbel']);
+        const uri = screenshotFile.get_uri();
+        const bookmarks = new GLib.BookmarksFile();
+        try {
+            bookmarks.load_from_file(recentFile);
+        } catch (e) {
+            if (!e.matches(GLib.BookmarkFileError.FILE_NOT_FOUND)) {
+                log(`Could not open recent file ${uri}: ${e.message}`);
+                return;
+            }
+        }
+
+        try {
+            bookmarks.add_application(uri, GLib.get_prgname(), 'gio open %u');
+            bookmarks.to_file(recentFile);
+        } catch (e) {
+            log(`Could not save recent file ${uri}: ${e.message}`);
+        }
+    }
+
     const lockdownSettings =
         new Gio.Settings({ schema_id: 'org.gnome.desktop.lockdown' });
     const disableSaveToDisk =
@@ -2102,7 +2129,7 @@ function _storeScreenshot(bytes, pixbuf) {
         }
 
         // Add it to recent files.
-        Gtk.RecentManager.get_default().add_item(file.get_uri());
+        saveRecentFile(file);
     }
 
     // Create a St.ImageContent icon for the notification. We want